<%@ page import="java.util.List" %>
<%@ page import="model.Department" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.ObjectInputStream" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.io.File" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>部门列表</title>
    <script src="./js/jquery.min.js"></script>
    <script>
        $(function (){
            $("#deleteSelected").click(function (){
                var length = $(".item:checked").length;
                if (length === 0) {
                    alert("请选择需要删除的内容");
                } else {
                    // 提醒下：你确定要删除部门信息吗？
                    if (confirm("你确定要删除部门信息吗？")) {
                        // 通过js进行提交
                        $("#formBatch").submit();
                    }
                }
            })
        })
    </script>
</head>
<body>
<form action="<%=request.getContextPath()%>/deleteSelected.action" id="formBatch" method="post">
    <table border="1px" width="600px" align="center" cellspacing="0" cellpadding="10px">
        <caption>部门列表</caption>
        <div>
            <button id="deleteSelected">批量删除 <span id="checkedCount">0</span></button>
        </div>
        <tr>
            <td colspan="6">
                <a href="${pageContext.request.contextPath}/addPart.jsp">添加数据</a>
                <input type="checkbox" id="selectAll"> 全选

            </td>
        </tr>
        <tr>
            <th>选择</th>
            <th>序号</th>
            <th>部门名称</th>
            <th>部门描述</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
        <%
            List<Department> partList;
            File file = new File("C:\\Users\\FeiFei\\Desktop\\test.txt");
            if (file.exists() && file.length() > 0) {

                try (FileInputStream fis = new FileInputStream(file);
                     ObjectInputStream ois = new ObjectInputStream(fis)) {
                    partList = (List<Department>) ois.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    partList = new ArrayList<>();
                }
            } else {
                partList = new ArrayList<>();
            }
            request.setAttribute("partList", partList);
            // 从流读取对象信息
        %>
        <%--    <input type="submit" id="">删除选中<br>--%>
        <c:if test="${partList != null and partList.size() != 0}">
            <c:forEach items="${partList}" var="part" varStatus="status">
                <tr>
                    <td><input type="checkbox" name="checkedID" class="item" value="${status.count-1}">${status.count-1}
                    </td>
                    <td>${status.count}</td>
                    <td>${part.departName}</td>
                    <td>${part.description}</td>
                    <td>${part.updateTime}</td>
                    <td>
                        <a href="${pageContext.request.contextPath}/editServlet.action?id=${status.index}">编辑</a>
                        <a href="${pageContext.request.contextPath}/deleteServlet.action?id=${status.index}">删除</a>
                    </td>
                </tr>
            </c:forEach>
        </c:if>
    </table>
</form>
<script>



    $("#selectAll").click(function () {
        //如果用attr  标签里面没有写  获取不到value值 有才能获取到
        //例：$(this).prop("checked"); 获取值为Undefined
        //用prop可以获取
        var prop = $(this).prop("checked");
        $(".item").attr("checked", prop);
        $("#checkedCount").text( $(".item:checked").length);
    })
    $(".item").click(function () {
        if ($(".item:checked").length === $(".item").length) {
            $("#selectAll").prop("checked", true)
        } else {
            $("#selectAll").prop("checked", false)
        }
        $("#checkedCount").text( $(".item:checked").length);
    });
</script>
</body>
</html>
